package com.vote.admin.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vote.admin.vo.result.ViewResult;
import com.vote.admin.vo.result.VisitResult;
import com.vote.common.entity.VoteProject;
import com.vote.common.entity.VoteVisit;
import com.vote.common.returns.RespCode;
import com.vote.common.returns.RespEntity;
import com.vote.common.service.VoteProjectService;
import com.vote.common.service.VoteVisitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AdminVisitService {

    @Autowired
    private VoteVisitService voteVisitService;

    public RespEntity<VisitResult> visitList() {
        //最近24小时访问量数据
        List<VoteVisit> hourVisitList = voteVisitService.listRecent24HVisit();

        //最近30日访问量数据
        List<VoteVisit> dayVisitList = voteVisitService.listRecent30DaysVisit();

        //数据组装
        VisitResult visitResult = dataPackage(hourVisitList, dayVisitList);

        return new RespEntity(RespCode.SUCCESS, visitResult);
    }

    /**
     * 数据组装
     * @param hourVisitList
     * @param dayVisitList
     * @return
     */
    private VisitResult dataPackage(List<VoteVisit> hourVisitList, List<VoteVisit> dayVisitList) {
        //近24小时日期集合
        List<String> hourDateList = hourVisitList.stream().map(VoteVisit::getDt).collect(Collectors.toList());
        hourDateList = hourDateList.stream().map(v -> {
            String[] split = v.split("\\s+")[1].split(":");
            return split[0] + ":" + split[1];
        }).collect(Collectors.toList());

        //近24小时访问量集合
        List<Integer> hourValueList = hourVisitList.stream().map(VoteVisit::getVisit).collect(Collectors.toList());
        //近24小时访问量最大值
        Integer hourMaxValue = hourValueList.stream().max(Integer::compareTo).get();

        //近30天日期集合
        List<String> dayDateList = dayVisitList.stream().map(VoteVisit::getDtD).collect(Collectors.toList());
        dayDateList = dayDateList.stream().map(v -> {
            String[] split = v.split("-");
            return split[1] + "-" + split[2];
        }).collect(Collectors.toList());

        //近30天访问量集合
        List<Integer> dayValueList = dayVisitList.stream().map(VoteVisit::getVisit).collect(Collectors.toList());
        //近30天访问量最大值
        Integer dayValueMax = dayValueList.stream().max(Integer::compareTo).get();

        return new VisitResult(hourMaxValue, hourDateList, hourValueList, dayValueMax, dayDateList, dayValueList);
    }

}
